﻿function SetComboxCtrlXmlData(listControl, displayNode, valueNode, XmlData) {
    var subData;

    if (XmlData == null)
        return;

    while (listControl[0].length > 0)
        listControl[0].remove(0);

    TraversalSubNode(XmlData, function (subData) {
        var oOption = document.createElement("OPTION");

        if (displayNode != "")
            oOption.text = XmlNodeManage.GetSubValue(subData, displayNode);

        if (valueNode != "")
            oOption.value = XmlNodeManage.GetSubValue(subData, valueNode);

        listControl[0].add(oOption);
    });
};

function SetCheckListBoxXmlData(checkListBox, displayNode, valueNode, XmlData) {
    if (XmlData == null || checkListBox == null)
        return;

    var subIDName = checkListBox.attr("id");

    var subData, strGroupCheckListHtml = "";
    var GroupCount = 0;
    for (var j = 0; j < XmlData.childNodes.length; j++) {
        subData = XmlData.childNodes[j];
        GroupCount++;
        strGroupCheckListHtml += "<input id='" + subIDName + GroupCount.toString() + "' type='checkbox' value='" + XmlNodeManage.GetSubValue(subData, valueNode) + "' />" + XmlNodeManage.GetSubValue(subData, displayNode) + "&nbsp;";
    }
    checkListBox.html(strGroupCheckListHtml);
};

function SetCheckListBoxXmlValueData(checkListBox, XmlData, tagName, valueNode) {
    if (XmlData == null || checkListBox == null)
        return;

    var subIDName = ":checkbox[id^='" + checkListBox.attr("id") + "']";
    var subData = null;
    var checkbos = null;

    if ($(subIDName).length > 0) {
        for (var i = 0; i < $(subIDName).length; i++) {
            $(subIDName)[i].checked = false;
        }

        for (var i = 0; i < XmlData.childNodes.length; i++) {
            subData = XmlData.childNodes[i];

            if (subData.tagName != tagName)
                continue;

            for (var j = 0; j < $(subIDName).length; j++) {
                checkbos = $(subIDName)[j];
                if (checkbos.value == XmlNodeManage.GetSubValue(subData, valueNode))
                    checkbos.checked = true;
            }
        }
    }
};

function GetXmlDataFormControlList(ControlList, XmlData) {
    if (XmlData == null || ControlList == null)
        return;

    for (var i = 0; i < ControlList.length; i++)
        ControlList[i].GetXmlData(XmlData);
};

function SetXmlDataToControlList(ControlList, XmlData) {
    if (XmlData == null || ControlList == null)
        return;

    for (var i = 0; i < ControlList.length; i++)
        ControlList[i].SetXmlData(XmlData);
};

function SwapControl(control, nodeName) {
    this.Control = control;
    this.NodeName = nodeName;
};

SwapControl.prototype.GetXmlData = function (XmlData) {
    XmlNodeManage.SetSubValue(XmlData, this.NodeName, null, this.Control.attr("value"));
};

SwapControl.prototype.SetXmlData = function (XmlData) {
    this.Control.attr("value", XmlNodeManage.GetSubValue(XmlData, this.NodeName));
};

function SwapCheckBoxControl(control, nodeName) {
    this.Control = control;
    this.NodeName = nodeName;
};

SwapCheckBoxControl.prototype.GetXmlData = function (XmlData) {
    if (this.Control.attr("checked"))
        XmlNodeManage.SetSubValue(XmlData, this.NodeName, null, "true");
    else
        XmlNodeManage.SetSubValue(XmlData, this.NodeName, null, "false");
};

SwapCheckBoxControl.prototype.SetXmlData = function (XmlData) {
    var sValue = XmlNodeManage.GetSubValue(XmlData, this.NodeName).toLowerCase();
    if (sValue == "true")
        this.Control.attr("checked", true);
    else
        this.Control.attr("checked", false);
};

function SwapCheckListBoxControl(control, nodeName) {
    this.Control = control;
    this.NodeName = nodeName;
    this.subIDName = ":checkbox[id^='" + this.Control.attr("id") + "']";
    this.subCheckIDName = "input:checked[id^='" + this.Control.attr("id") + "']";
};

SwapCheckListBoxControl.prototype.GetXmlData = function (XmlData) {
    var strResult = "";
    var checkbos = null;
    if ($(this.subCheckIDName).length > 0) {
        for (var i = 0; i < $(this.subCheckIDName).length; i++) {
            checkbos = $(this.subCheckIDName)[i];
            strResult += ";" + checkbos.value;
        }
    }
    return strResult.substr(1);
};

SwapCheckListBoxControl.prototype.SetXmlData = function (XmlData) {
    var sValue = XmlNodeManage.GetSubValue(XmlData, this.NodeName);
    var ssValueArray = sValue.split(";");
    var ssValue;
    var subData = null;
    var checkbos = null;

    if ($(this.subIDName).length > 0) {
        for (var i = 0; i < $(this.subIDName).length; i++) {
            checkbos = $(this.subIDName)[i];
            checkbos.checked = false;
        }

        for (var i = 0; i < ssValueArray.length; i++) {
            ssValue = ssValueArray[i];

            for (var j = 0; j < $(this.subIDName).length; j++) {
                checkbos = $(this.subIDName)[j];
                if (checkbos.value == ssValue)
                    checkbos.checked = true;
            }
        }
    }
};
